//
//  AppNetwork.h
//  liangyi
//
//  Created by 张水 on 16/12/12.
//  Copyright © 2016年 yswh. All rights reserved.
//
#import "AppNetwork.h"


@interface AppNetwork ()

@property (retain, nonatomic) AFHTTPSessionManager *manager;

@end

@implementation AppNetwork

+ (void)requestWithPath:(NSString *)url
             parameters:(id)parameters
                success:(void(^)(id responseObject))success
                failure:(void(^)(id error))failure {
    
    [[self sharedAppNetwork] requestWithPath:url parameters:parameters success:success failure:failure];
}

- (void)requestWithPath:(NSString *)url
             parameters:(id)parameters
                success:(void(^)(id responseObject))success
                failure:(void(^)(id error))failure {
    
    
    [self.manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
    
}

+ (AppNetwork *)sharedAppNetwork {
    
    
    static AppNetwork *network = nil;
    static dispatch_once_t sharedAppNetwork;
    
    dispatch_once(&sharedAppNetwork, ^{
        
        if (!network) {
            
            network = [[AppNetwork alloc] init];
            
            network.manager = [AFHTTPSessionManager manager];
            
            //json解析格式
            network.manager.responseSerializer = [AFJSONResponseSerializer serializer];
            network.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/xml",@"text/plain", @"application/javascript", nil];
            
            //延迟时长
            [network.manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
            network.manager.requestSerializer.timeoutInterval = 10.0f;
            [network.manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
            
        };
        
    });
    
    return network;
}



@end

